<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Config;
use app\common\model\User;
use app\common\model\video\Item as VideoItem;
/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
  
        $todayTime = strtotime(date('Y-m-d'));
        $senventodayTime = strtotime(date('Y-m-d', strtotime("-7 days")));
        $hooks = config('addons.hooks');
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
        $addonComposerCfg = ROOT_PATH . '/vendor/karsonzhang/fastadmin-addons/composer.json';
        Config::parse($addonComposerCfg, "json", "composer");
        $config = Config::get("composer");
        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');

        $line = $this->getOrderLine();

        $this->view->assign([
            'totaluser'        => User::cache(60)->count(),
            'totalVideo'       => VideoItem::cache(60)->count(),
            'totalviews'       => 219390,
            'totalorder'       => 32143,
            'totalorderamount' => 174800,
            'todayuserlogin'   => User::cache(60)->where('logintime','egt',$todayTime)->count(),
            'todayusersignup'  => User::cache(60)->where('createtime','egt',$todayTime)->count(),
            'todayorder'       => 2324,
            'unsettleorder'    => 132,
            'sevendnu'         => User::cache(60)->where('createtime','egt',$senventodayTime)->count(),
            'sevendau'         => User::cache(60)->where('logintime','egt',$senventodayTime)->count(),
            'addonversion'       => $addonVersion,
            'uploadmode'       => $uploadmode,
            'date'             => $line['date'],
            'paylist'          => $line['amount'],
            'users'            => $line['users'],
            'createlist'       => $line['order'],
            'com'              => $line['com'],
        ]);

        return $this->view->fetch();
    }


    protected function getOrderLine($start = 0, $end = 0){
        if($start == 0){
            $start = strtotime('-90 days');
        }
        if($end == 0){
            $end = time();
        }
        $days = [];
        $now = $start;
        for ($i = 0; $now <= $end; $i++) {
            $now = $start + ($i * 86400);
            $days[] = date("Y-m-d", $now);
        }
        $orders = [];
        $users = [];
        $amount = [];
        $com = [];
        foreach ($days as $key => $value) {
            $t_s = strtotime($value);
            $t_e = $t_s + 86399;
            // $orders[] = OrderModel::cache(10)
            //     ->where('trade_status','1')
            //     // ->where('trade_type', 'neq', OrderService::VIP_ORDER )
            //     ->where('paytime','between',[$t_s, $t_e])
            //     ->count();
            // $amount[] = OrderModel::cache(10)
            // ->where('trade_status','1')
            // // ->where('trade_type', 'neq', OrderService::VIP_ORDER )
            // ->where('paytime','between',[$t_s, $t_e])
            // ->sum('trade_amount');
            $users[] = User::cache(10)
            ->where('jointime','elt', $t_e)
            ->count();

            $com[] = 0;
        }

        return [
            'date' => $days,
            'order' => $orders,
            'amount' => $amount,
            'users' => $users,
            'com' => $com,
        ];
    }
}
